<?php

namespace addons\shopro\controller;
use think\Db;
class OrderAftersale extends Base
{

    protected $noNeedLogin = ["two_hour_aftersale","del_test_user"];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $params = $this->request->get();
        $params['type'] = input('type');
        $params['offset'] = input('offset');

        $this->success('售后列表', \addons\shopro\model\OrderAftersale::getList($params));
    }


    /**
     * 详情
     */
    public function detail()
    {
        $params['id'] = input('id');
        $this->shoproValidate($params, get_class(), 'detail');

        $this->success('售后详情', \addons\shopro\model\OrderAftersale::detail($params));
    }



    // 申请售后
    public function aftersale()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'aftersale');

        $this->success('申请成功', \addons\shopro\model\OrderAftersale::aftersale($params));
    }


    // 2小时后自动取消订单，权益收回
    public function two_hour_aftersale()
    {
        $where["share_retreat"]=0;
        $where["o.status"]=1;
        $today_0=strtotime(date("Y-m-d",time()));
        $before_two_hour=time()-2*60*60;
        //$before_two_hour=time()-60;
        $two_hours_orders=\addons\shopro\model\Order::alias("o")
                          ->join("shopro_order_item t","t.order_id=o.id", 'LEFT')
                          ->join("user u","u.id=o.user_id", 'LEFT')
                          ->where($where)
                          ->where("pay_fee",'>',0)
                          ->where("paytime",'>',$today_0)
                          ->where("paytime",'<',$before_two_hour)
                          ->where("u.staff",2)
                          ->field("o.*,t.order_id,t.id as order_item_id,u.staff")
                          ->select();
        //var_dump($two_hours_orders);die;
        if(!empty($two_hours_orders)){
            foreach($two_hours_orders as $k=>$v){
                $params["user_id"]=$v["user_id"];
                $params["refund_fee"]=$v["total_fee"];
                $params["order_id"]=$v["id"];
                $params["order_item_id"]=$v["order_item_id"];
                $params["content"]="超过两小时，自动退货";
                $res11=\addons\shopro\model\OrderAftersale::aftersale_two_hour($params);
            }
        }
        $this->success('成功');

    }

    // 取消售后单
    public function cancel()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'cancel');

        $this->success('取消成功', \addons\shopro\model\OrderAftersale::operCancel($params));
    }

    // 删除售后单
    public function delete()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'delete');

        $this->success('删除成功', \addons\shopro\model\OrderAftersale::operDelete($params));
    }

    // 填写快递单号
    public function express()
    {
        $id = input('id');
        $express_no = input('express_no');
        if (empty($express_no)) {
            $this->error('快递单号不能为空');
        }
        $info = db('shopro_order_aftersale')->where('id',$id)->find();
        if (!$info) {
            $this->error('售后信息不存在');
        }
        $up = db('shopro_order_aftersale')->where('id',$id)->update(['express_no'=>$express_no]);
        if ($up) {
            $this->success('提交成功');
        }
        $this->error('提交失败');
    }

}
